//
//  CZShareController.m
//  A01-CZ彩票
//
//  Created by apple on 15/6/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZShareController.h"
#import <MessageUI/MessageUI.h>
@interface CZShareController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation CZShareController

- (void)dealloc{
    NSLog(@"dealloc");
}


- (UIWebView *)webView{
    if (_webView == nil) {
        _webView = [UIWebView new];
        
    }
    return _webView;
}



- (void)setData{
    
    
//    __weak CZShareController *weakSelf = self;
    
    __weak typeof(self) weakSelf = self;
    
    CZItem *item1 = [CZItemArrow itemWithTitle:@"电话分享" icon:nil option:^{
        //通话完成会回到当前应用， 以前的时候不会回到当前应用
//        NSURL *url = [NSURL URLWithString:@"tel://54188"];
//        [[UIApplication sharedApplication] openURL:url];
        
        
        //打电话之前会有提示  回到当前应用   私有的api
//        NSURL *url = [NSURL URLWithString:@"telprompt://54188"];
//        [[UIApplication sharedApplication] openURL:url];
        
        NSURL *url = [NSURL URLWithString:@"tel://54188"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [weakSelf.webView loadRequest:request];
        
    }];
    
    CZItem *item2 = [CZItemArrow itemWithTitle:@"短信分享" icon:nil option:^{
        
        //发完短信之后。会到短信界面
//        NSURL *url = [NSURL URLWithString:@"sms://54188"];
//        [[UIApplication sharedApplication] openURL:url];
        
        
        //判断设备是否能发送信息
        if (![MFMessageComposeViewController canSendText]) {
            return;
        }
        
        MFMessageComposeViewController *vc = [MFMessageComposeViewController new];
        //收件人列表
        vc.recipients = @[@"10000",@"10086"];
        vc.body = @"推荐一个nb的游戏 http://www.nlcoder.com/test";
        vc.subject = @"biaoti";
        //设置代理
        vc.messageComposeDelegate = weakSelf;
        
        [weakSelf presentViewController:vc animated:YES completion:nil];
        
    }];
    
    CZItem *item3 = [CZItemArrow itemWithTitle:@"邮件分享" icon:nil option:^{
        
        //判断是否能发送邮件
        if (![MFMailComposeViewController canSendMail]) {
            return;
        }
        
        MFMailComposeViewController *vc = [MFMailComposeViewController new];
        
        vc.mailComposeDelegate = weakSelf;
        //设置收件人
        [vc setToRecipients:@[@"1111@itcast.cn",@"2222@itcast.cn"]];
        //密送
//        [vc setBccRecipients:<#(NSArray *)#>]
        //抄送
//        [vc setCcRecipients:<#(NSArray *)#>]
        
        [vc setSubject:@"收福利了"];
        [vc setMessageBody:@"送美女" isHTML:NO];
        
        //
        UIImage *img = [UIImage imageNamed:@"aa"];
        NSData *data = UIImagePNGRepresentation(img);
        
        [vc addAttachmentData:data mimeType:@"image/png" fileName:@"cls.png"];
        
        
        
        [weakSelf presentViewController:vc animated:YES completion:nil];
        
    }];
    
    //controller(self) --> self.groups -->  group  -->  item   --> option  --> self
    
    CZGroup *group = [CZGroup groupWithItems:@[item1,item2,item3]];
    self.groups = @[group];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller  dismissViewControllerAnimated:YES completion:nil];

}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [controller  dismissViewControllerAnimated:YES completion:nil];
}

@end
